﻿#include "stdafx.h"
#include "SSwitch2.h"

namespace SOUI
{

	SSwitch2::SSwitch2() : m_BOpen(TRUE), m_BChangeing(FALSE), m_FrameNow(0), m_pSkin(NULL), m_pSkinOff(NULL), m_pSkinOn(NULL)
	{
		m_style.m_strCursor = _T("hand");
		m_evtSet.addEvent(EVENTID(EventSwitchChanging));
	}

	SSwitch2::~SSwitch2()
	{

	}

	void SSwitch2::GetTextRect(LPRECT pRect)
	{
		GetClientRect(pRect);
		SASSERT(m_pSkin);
		CSize szSwitch = m_pSkin->GetSkinSize();
		pRect->left += szSwitch.cx + SwitchSpacing;
	}

	CRect SSwitch2::GetSwitchRect()
	{
		CRect rcClient;
		GetClientRect(rcClient);
		SASSERT(m_pSkin);
		CSize szCheck = m_pSkin->GetSkinSize();
		CRect rcCheckBox(rcClient.TopLeft(), szCheck);
		rcCheckBox.OffsetRect(0, (rcClient.Height() - szCheck.cy) / 2);
		return rcCheckBox;
	}

	void SSwitch2::OnPaint(IRenderTarget *pRT)
	{
		//背景、前景皮肤中间状态数必须一致。自己保证，不再代码检查
		CRect rcSwitch =GetSwitchRect();
		if (IsDisabled(TRUE))
		{
			if (m_disableSkin == NULL)
			{
				if(m_disableSkin=m_pSkin->Scale(GetScale()))
					m_disableSkin->OnColorize(0XFF888888);
			}
			if (m_disableSkin)
				m_disableSkin->Draw(pRT, rcSwitch, m_BOpen ? m_FrameNow : m_pSkin->GetStates() - m_FrameNow - 1);
		}
		else
		{
			if (m_pSkin)
				m_pSkin->Draw(pRT, rcSwitch, m_BOpen ? m_FrameNow : m_pSkin->GetStates() - m_FrameNow - 1);
		}
		if (!m_pSkinOn || !m_pSkinOff)
			return;
		(m_BOpen ?m_pSkinOn:m_pSkinOff)->Draw(pRT, rcSwitch, m_FrameNow);
		__super::OnPaint(pRT);
	}
	
	CSize SSwitch2::GetDesiredSize(LPCRECT pRcContainer)
	{
		CSize szSkinRet;
		if (m_pSkin)
			szSkinRet = m_pSkin->GetSkinSize();
		CSize szRet = __super::GetDesiredSize(pRcContainer);
		szRet.cx += szSkinRet.cx + SwitchSpacing;
		szRet.cy = (std::max)(szRet.cy, szSkinRet.cy);
		return szRet;
	}

	HRESULT SSwitch2::OnAttrOpen(const SStringW& strValue, BOOL bLoading)
	{
		m_BOpen = (strValue != L"0");
		m_FrameNow =  0;
		if (!bLoading)
			SetOpen(m_BOpen);
		
		return bLoading ? S_FALSE : S_OK;
	}

	void SSwitch2::OnLButtonUp(UINT nFlags, CPoint point)
	{
		__super::OnLButtonUp(nFlags, point);
		if (!GetWindowRect().PtInRect(point)) return;
		SetOpen(!m_BOpen);
	}

	void SSwitch2::OnKeyDown(UINT nChar, UINT nRepCnt, UINT nFlags)
	{
		if (nChar == VK_SPACE)
		{
			SetOpen(!m_BOpen);
		}
	}
	
	//属性处理

	BOOL SSwitch2::IsOpen() { return m_BOpen; }

	void SSwitch2::SetOpen(BOOL bOpen)
	{
		if ((m_BOpen != bOpen) && !m_BChangeing)
		{
			EventSwitchChanging evt(this);
			evt.bTurnOn = bOpen;
			FireEvent(evt);

			if (evt.bCacnel)
				return;
			m_BOpen = bOpen;
			m_BChangeing = TRUE;
			m_FrameNow = m_pSkinOn->GetStates()-1;
			GetContainer()->RegisterTimelineHandler(this);			
		}
	}

	void SSwitch2::OnStateChanged(DWORD dwOldState, DWORD dwNewState)
	{
		InvalidateRect(NULL);
		__super::OnStateChanged(dwOldState, dwNewState);
	}

	void SSwitch2::OnNextFrame()
	{
		if (m_BChangeing&&m_pSkinOn)
		{
			m_FrameNow--;
			if (m_FrameNow <=0)
			{
				m_FrameNow = 0;
				m_BChangeing = FALSE;
				GetContainer()->UnregisterTimelineHandler(this);
			}
		}
		else
			GetContainer()->UnregisterTimelineHandler(this);
		InvalidateRect(NULL);
	}
}